#include <kernel/bus.h>
#include <kernel/device.h>
#include <kernel/driver.h>
#include <kernel/kobject.h>
#include <kernel/initcall.h>
#include <kernel/syslog.h>
#include <mm/mem.h>
#include <drivers/platform.h>
#include <drivers/spi.h>


static bool kobj_print_current_dir(kobj_t *kobj)
{
    kobj_t *kobj_node;
    struct list_node *head = &kobj->child;

    if(kobj_is_dir(kobj)) {
        list_for_each_entry(kobj_node, head, kobj_t, entry) {
            SYSLOG_INFO("%s ", kobj_node->name);
        }
    }
}


static bool kobj_foreach_all(kobj_t *kobj)
{
    if(kobj == NULL) {
        return false;
    }

    kobj_t *kobj_node;
    struct list_node *head = &kobj->child;

    if(kobj_is_dir(kobj)) {
        list_for_each_entry(kobj_node, head, kobj_t, entry) {
            kobj_foreach_all(kobj_node);
        }
    }

    SYSLOG_INFO("%s", kobj->name);
    return true;
}


static struct spi_device spi_dev = {
    .device = {
        .name = "w25w128",
    },
};


static int test_probe(struct spi_device *dev)
{
    SYSLOG_INFO("probe ok");
    return 0;
}


static void test_remove(struct spi_device *dev)
{
    SYSLOG_INFO("remove ok");
}


static struct match_id test_match[] = {
    {.compatible = "w25w64", .data = "test"},
    {.compatible = "w25w128", .data = "test1"},
    { }
};


static struct spi_driver spi_drv = {
    .driver = {
        .name = "w25wxx",
    },
    .probe = test_probe,
    .remove = test_remove,
    .id_table = test_match,
};


int test_spi_register(void)
{
    kobj_t *root = kobj_get_root();

    spi_device_register(&spi_dev);
    spi_driver_register(&spi_drv);
    
    kobj_t *pkobj = kobj_find_by_name(NULL, "spi");
    if(pkobj == NULL) {
        SYSLOG_INFO("spi bus not found");
        return 0;
    }

    kobj_t *dev_kobj = kobj_find_by_name(pkobj, "drivers");
    if(dev_kobj == NULL) {
        SYSLOG_INFO("device bus not found");
        return 0;
    }

    //kobj_print_current_dir(root);

    return 0;
}


module_initcall(test_spi_register);
